#include <iostream>
#include <limits>  // std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
#include <sstream> // sstream << setw()
#include <iomanip> // setfill('0') setw(2)
#include <random>  // 随机数
#include <ctime>

const int range_red_ball = 33;
const int range_blue_ball = 16;
const int count_ball = 7;
const int count_red_ball = 6;
const int count_blue_ball = 1;

// 读取用户在控制如上输入的一个号码
int get_number_input()
{
    int input = -1;

    std::cin >> input;

    while (std::cin.fail())
    {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cin >> input;
    }
    return input;
}

// 机选号码
int *get_random_balls()
{

    int *balls = new int[count_ball];

    int *datapool = new int[range_red_ball];

    for (int i = 0; i < range_red_ball; i++)
    {
        datapool[i] = i + 1;
    }

    for (int i = 0; i < count_red_ball; i++)
    {
        int l = range_red_ball - i;
        int rand_index = rand() % l;
        balls[i] = datapool[rand_index];

        // 换数据源
        int *temp = new int[l - 1];
        for (int i = 0, itemp = 0; i < l; i++)
        {
            if (rand_index == i)
            {
                continue;
            }
            temp[itemp++] = datapool[i];
        }
        delete datapool;
        datapool = temp;
    }

    balls[count_ball - 1] = rand() % 16 + 1;

    return balls;
}

void sort_array(int *array, int len)
{
    for (int i = len - 1; i >= 0; i--)
    {
        for (int j = 0; j < i; j++)
        {
            if (array[j + 1] < array[j])
            {
                int temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }

    // std::cout << "排序函数：" << std::endl;
    // for (int i = 0; i < len; i++)
    // {
    //     std::cout << array[i] << "-";
    // }
}

std::string desc_union_ball(int *array)
{
    std::stringstream sstream;
    sstream << "红球：";
    for (int i = 0; i < count_ball; i++)
    {
        sstream << std::setw(2) << std::setfill('0') << array[i];
        if (i < count_red_ball - 1)
        {
            sstream << "， ";
        }
        else if (i == count_red_ball - 1)
        {
            sstream << "， 蓝球：";
        }
    }
    return sstream.str();
}

// 自选号码
int *get_custom_balls()
{

    int *balls_array = new int[count_ball];

input_red_ball:
    std::cout << "请输入 " << count_red_ball << " 个红球：";
    for (int i = 0; i < count_red_ball; i++)
    {
        balls_array[i] = get_number_input();
    }

    // sort 用来排序
    sort_array(balls_array, count_red_ball);

    if (balls_array[0] < 1 || balls_array[count_red_ball - 1] > range_red_ball)
    {
        std::cout << "双色球红球越界了(1~" << range_red_ball << ")，请重新输入！" << std::endl;
        goto input_red_ball;
    }

    for (int i = 0; i < count_red_ball - 1; i++)
    {
        if (balls_array[i] == balls_array[i + 1])
        {
            std::cout << "双色球红球重复，请重新输入！" << std::endl;
            goto input_red_ball;
        }
    }

    // std::cout << "红色球输入完成" << std::endl;
    // for (int i = 0; i < count_red_ball; i++)
    // {
    //     std::cout << balls_array[i] << " ";
    // }
    // std::cout << std::endl;

input_blue_ball:
    std::cout << "请输入 " << count_blue_ball << " 个蓝球：";
    balls_array[count_ball - 1] = get_number_input();

    if (balls_array[count_ball - 1] < 0 || balls_array[count_ball - 1] > range_blue_ball)
    {
        std::cout << "双色球蓝球越界(1~" << range_blue_ball << ")，请重新输入！" << std::endl;
        goto input_blue_ball;
    }

    // std::cout << "蓝色球输入完成" << std::endl;

    // std::cout << "无敌天宝将军宇文成都" << std::endl;
    // for (int i = 0; i < count_ball; i++)
    // {
    //     std::cout << balls_array[i] << " ";
    // }
    // std::cout << std::endl;
    // std::cout << "锏打三州六府，马踏黄河两岸，威震山东半边天的小孟尝秦琼秦叔宝" << std::endl;

    return balls_array;
}

void print_menu()
{
    using namespace std;

    cout << "*************************" << endl;
    cout << "欢迎来到双色球！\n请输入您的选择：" << endl;
    cout << "1. 自选号码" << endl;
    cout << "2. 机选号码" << endl;
    cout << "0. 不玩儿了" << endl;
    cout << "*************************" << endl;
}

int get_bonus_level(int *user, int *system)
{
    int red_pool[range_red_ball + 1];
    int red_count = 0;
    const int flag = 1;
    for (int i = 0; i < count_red_ball; i++)
    {
        red_pool[user[i]] = flag;
    }

    for (int i = 0; i < count_red_ball; i++)
    {
        if (red_pool[system[i]] == flag)
        {
            red_count++;
        }
    }

    int blue_count = system[count_ball - 1] == user[count_ball - 1];
    int result = 7;
    if (red_count == 6 && blue_count == 1)
    {
        result = 1;
    }
    else if (red_count == 6)
    {
        result = 2;
    }
    else if (red_count == 5 && blue_count == 1)
    {
        result = 3;
    }
    else if (red_count + blue_count == 5)
    {
        result = 4;
    }
    else if (red_count + blue_count == 4)
    {
        result = 5;
    }
    else if (blue_count == 1)
    {
        result = 6;
    }
    return result;
}

int get_bonus_number(int level)
{
    switch (level)
    {
    case 1:
        return 5000000;
    case 2:
        return 800000;
    case 3:
        return 3000;
    case 4:
        return 200;
    case 5:
        return 10;
    case 6:
        return 5;
    default:
        return 0;
    }
}

void play_game()
{

    int times = 0;
    int bonus_time = 0;
    int bonus_sum = 0;
    int consume = 0;

    int user_choice = -1;

    bool flag = true;
    while (flag)
    {
        print_menu();
        user_choice = get_number_input();

        int *user_balls = NULL;
        switch (user_choice)
        {
        case 1: // 自选号码
            user_balls = get_custom_balls();
            break;
        case 2: // 机选号码
            user_balls = get_random_balls();
            break;
        case 0:
            flag = false;
            break;
        default:
            continue;
        }

        if (!flag)
            break;

        times++;
        consume += 14;

        std::cout << std::endl;
        sort_array(user_balls, count_red_ball);
        std::cout << "用户选择的是：\t" << desc_union_ball(user_balls) << std::endl;

        int *system_balls = get_random_balls();
        sort_array(system_balls, count_red_ball);
        std::cout << "中奖的号码是：\t" << desc_union_ball(system_balls) << std::endl;
        std::cout << std::endl;

        int level = get_bonus_level(user_balls, system_balls);
        int bonus = get_bonus_number(level);

        if (level == 7)
        {
            std::cout << "很遗憾，您没有中奖，再接再励！" << std::endl;
        }
        else
        {
            bonus_sum += bonus;
            bonus_time++;
            std::cout << "恭喜您，获得了【" << level << "】等奖，奖金为【" << bonus << "】元 !" << std::endl;
        }

    }

    std::cout << "本次您总共玩儿了【" << times << "】次，中奖了【" << bonus_time << "】次，中奖率为 "
              << (double)bonus_time / (double)times * 100 << "%" << std::endl;
    std::cout << "本次您总共消费了【" << consume << "】元，中奖了【" << bonus_sum << "】元，收支回报率为 "
              << (double)bonus_sum / (double)consume * 100 << "%" << std::endl;
}

int b = 0;

int a = 0;

int main(int argc, char const *argv[])
{

    srand(time(0));

    play_game();

    return 0;
}
